<?php
namespace Admin\Model;
use Think\Model;
class GoodsModel extends Model {
	/**
	 * 获得商品列表
	 * @param array $field 查询字段
	 * @param array $where 查询条件
	 * @param array $order 排序条件
	 * @return array 数据
	 */
	public function getList($field,$where,$order='GoodId asc'){
		//准备查询条件
		if($where['goods.GoodcategoryId']<=0){
			unset($where['goods.GoodcategoryId']);    //销毁where数组
		}
		if($where['Status']<=0){
			unset($where['Status']);
		}
		if($where['Is_best']<=0){
			unset($where['Is_best']);
		}
		//查询数据
		$count = $this->where($where)->count();
		$Page = new \Think\Page($count,7);
		$limit = $Page->firstRow.','.$Page->listRows;
		//取得数据
		$data['page'] = $Page->show();
		$data['list'] = M('goods')->join('join goodcategory ON goods.GoodcategoryId=goodcategory.GoodcategoryId')->field($field)->limit($limit)->order($order)->where($where)->select();
		return $data;
	}
	/**
	 * 上传文件
	 * @param $GoodId int 商品ID
	 * @return string 错误信息（成功返回true）
	 */
	/**
	 * 上传文件
	 * @param $GoodId int 商品ID
	 * @return string 错误信息（成功返回true）
	 */
	public function uploadThumb($GoodId,$Color,$Version){
		//准备上传目录
		$file['temp'] = './Public/uploads/temp/';
		file_exists($file['temp']) or mkdir($file['temp'],0777,true);
		//上传文件
		$Upload = new \Think\Upload(array(
			'exts' => array('jpg','png','jpeg'),
			'rootPath' => $file['temp'],
			'autoSub' => false,
		));
		$rst = $Upload->upload();
		if($rst===false){
			return $Upload->getError();
		}
		//生成文件信息
		$file['name'] = $rst['img']['savename'];
		$file['save'] = date('Y-m/d/');
		$file['path1'] = './Public/uploads/'.$file['save'];
		$file['path2'] = './Public/uploads/img/'.$file['save'];
		//创建保存目录
		file_exists($file['path1']) or mkdir($file['path1'],0777,true);
		file_exists($file['path2']) or mkdir($file['path2'],0777,true);
		//生成缩略图
		$Image = new \Think\Image();
		$Image->open($file['temp'].$file['name']);
		$Image->thumb(350,300,2)->save($file['path1'].$file['name']);//大图
		$Image->open($file['temp'].$file['name']);
		$Image->thumb(176,120,2)->save($file['path2'].$file['name']);//小图
		//删除临时文件
		unlink($file['temp'].$file['name']);
		//删除原来的图片文件
		$this->delImage($GoodId,$Color,$Version);
		//保存缩略图
		$where['GoodId']=$GoodId;
		$where['Color']=$Color;
		$where['Version']=$Version;
		$this->where($where)->save(array(
			'PictureUrl'=> $file['save'].$file['name'],
		));
		return true;
	}
	/**
	 * 删除商品关联图片文件
	 * @param type $GoodId
	 * @param type $file
	 */
	public function delImage($GoodId=0,$Color=0,$Version=0,$file=''){
		$path = './Public/uploads/';
		if($file==''){
			$where['GoodId']=$GoodId;
			$where['Color']=$Color;
			$where['Version']=$Version;
			$file = M('goods')->where($where)->getField('PictureUrl');
		}
		if($file && strlen(trim($file))>4){
			//删除文件（空目录仍然存在，需要用其他办法清理空目录）
			unlink($path.$file);
			unlink($path.'img/'.$file);
		}
	}
	/**
	 * 删除商品及关联的文件
	 */
	public function delAll($GoodId,$Color,$Version){
		//删除商品图片删除
		$this->delImage($GoodId,$Color,$Version);
		//删除商品
		return $this->where("GoodId=$GoodId AND Color=$Color AND Version=$Version")->delete();
	}
}
